﻿<%@ WebHandler Language="C#" Class="Notifications" %>

using System;
using System.Web;
using System.Web.UI;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public class Notifications : IHttpHandler {
    
    String sxOutput = String.Empty;
    
    public void ProcessRequest (HttpContext ctx) {
        String sxAction = ctx.ReadQuery("action");

        switch (sxAction.ToUpper())
        {
            case "UPDATE":
                sxOutput = UpdateNotifCount().ToString();
                sxOutput += KGlobal.DELIMITER + UpdateEventCount().ToString(); 
                break;
            case "NOTIF": sxOutput = GetNotifContent(); break;
            case "EVENT": sxOutput = GetEventContent(); break;
        }

        ctx.Response.ContentType = "text/plain";        
        ctx.Response.Write(sxOutput);
    }

    private int UpdateNotifCount()
    {
        Cached.NotificationTable ccNotif = new Cached.NotificationTable();
        return ccNotif.GetNotificationCount(ICPUser.ID);
    }

    private String GetNotifContent()
    {
        String sxReturn = String.Empty;

        Cached.NotificationTable ccNotif = new Cached.NotificationTable();

        foreach (NotificationData nfData in ccNotif.GetNotificationList(ICPUser.ID, KGlobal.DEFAULT_FEED_COUNT, true))
        {
            sxReturn += nfData.BuildNotificationItem();
        }

        DBQuery.NotificationTable.SetNotificationAsRead(ICPUser.ID);
        return sxReturn;
    }

    private int UpdateEventCount()
    {

        Cached.NotificationTable ccNotif = new Cached.NotificationTable();
        return ccNotif.GetEventCount(ICPUser.ID);
    }

    private String GetEventContent()
    {
        String sxReturn = String.Empty;

        Cached.NotificationTable ccNotif = new Cached.NotificationTable();

        foreach (UserEventData ueData in ccNotif.GetEventList(ICPUser.ID, true))
        {
            sxReturn += ueData.BuildUserEventItem();
        }

        return sxReturn;
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    

}